package main;

import java.util.ArrayList;

import adapter.AExternalSystem;

import hospital.Hospital;
import movement.Location;
import unit.Ambulance;
import unit.FireTruck;
import unit.PoliceCar;
import unit.Unit;
import dispatch.DispatchCenter;
import dispatch.Dispatcher;
import dispatch.Operator;
import emergency.*;
import emergency.Fire.Size;

public class World {
	
	private DispatchCenter dispatchCenter=null;
	
	@SuppressWarnings("static-access") 
	public World(String args[]){
		try{
			StdIn in = new StdIn(args[0]);
			String[] command=null;
			while (!in.isEmpty()) {
				
				String s = in.readLine();
				command = s.split(",");
				if(command[0].equalsIgnoreCase("DispatchCenter")){
					setDispatchCenter(new DispatchCenter(/*"Hoofddispatch"*/command[1], new Location(20,10,"HD"), new Location(20, 20, "HD-garage")));
					//getDispatchCenter().setName(command[1]);
				}
				
				else if(command[0].equalsIgnoreCase("hospital")){
					Location temp = new Location(command[2],command[3],command[1]);
					getDispatchCenter().getGarage().getHospitalboard().addHospital(new Hospital(command[1], temp));
				}
				
				else if(command[0].equalsIgnoreCase("firetruck")){
					Location temp = new Location(command[2],command[3],null);
					Fire.Size tempSize = Fire.Size.LOCAL;
					if(command[5].equalsIgnoreCase("house"))
						tempSize = Fire.Size.HOUSE;
					if(command[5].equalsIgnoreCase("facility"))
						tempSize = Fire.Size.FACILITY;
					Unit unit = new FireTruck(command[1],Integer.parseInt(command[4]), getDispatchCenter().getGarage(), tempSize);
					unit.setCurrentLocation(temp);
				} 
	
				else if(command[0].equalsIgnoreCase("policecar")){
					Location temp = new Location(command[2],command[3],null);
					Unit unit = new PoliceCar(command[1],Integer.parseInt(command[4]), getDispatchCenter().getGarage());
					unit.setCurrentLocation(temp);
				} 
				
				else if(command[0].equalsIgnoreCase("ambulance")){
					Location temp = new Location(command[2],command[3],null);
					Unit unit = new Ambulance(command[1],Integer.parseInt(command[4]), getDispatchCenter().getGarage());
					unit.setCurrentLocation(temp);
				} 	
			}
		}
		catch(Exception e){
			generateWorld();
		}
		if(!validWorld()){
			generateWorld();
		}
	}
	
	//TODO standaardwereld aanmaken
	private void generateWorld(){
		DispatchCenter dc = new DispatchCenter("DummyDC", new Location(20, 30, "DummyDCLocation"), new Location(25, 30, "DummyGarage"));
		setDispatchCenter(dc);
	}
	
	public DispatchCenter getDispatchCenter(){
		return dispatchCenter;
	}
	
	private void maakDummyDispatchers() {
		dispatchCenter.addDispatcher(new Dispatcher("Simon"));
		dispatchCenter.addDispatcher(new Dispatcher("Niels"));

	}
	
	@SuppressWarnings("unused")
	private void maakDummyEmergencies() {
		Severity s = Severity.NORMAL;
		Severity s2 = Severity.URGENT;
		movement.Location l = new movement.Location(100, 150, "test emergency 1");
		movement.Location l2 = new movement.Location(400, 210, "test emergency 2");
		movement.Location l3 = new movement.Location(-123, 130, "test emergency 3");
		TrafficAccident newTrafficAccident = new TrafficAccident(l3, s2, getDispatchCenter(), 2, 2);
		PublicDisturbance newPublicDisturbance = new PublicDisturbance(new Location(10, 10, "loc"), Severity.SERIOUS, getDispatchCenter(), 9);
		Fire newFire = new Fire(l, s, getDispatchCenter(), Size.LOCAL, true, 2, 1);
		Robbery newRobbery = new Robbery(l3, s, getDispatchCenter(), true, true);
		
		Fire newFire2 = new Fire(l2, s2, getDispatchCenter(), Size.FACILITY,true, 3, 1);
		ArrayList<Emergency> emergencies = new ArrayList<Emergency>();
		emergencies.add(newFire2);emergencies.add(newRobbery);
		Disaster newDisaster = new Disaster(getDispatchCenter(), emergencies, "Simon wat doet ge toch");
		
	}

	private void maakDummyOperators() {
		dispatchCenter.addOperator(new Operator("Jelle"));
		dispatchCenter.addOperator(new Operator("Vincent"));
	}
	
	public void setDispatchCenter(DispatchCenter dc){
		dispatchCenter=dc;
	}
    
	protected void startup() {
		maakDummyOperators();
		maakDummyDispatchers();
		maakDummyEmergencies();

		//maak verbinding met extern systeem
		AExternalSystem exsys = new AExternalSystem();
		getDispatchCenter().getClock().addObserver(exsys);

	}

	private Boolean validWorld(){
		return dispatchCenter!=null;
	}
	
/*	public void maakDummyHospitals(){
		dispatchCenter.getGarage().getHospitalboard().getHospitals().add(new Hospital("Gasthuisberg", new Location(50, 50, "GHB")));
		dispatchCenter.getGarage().getHospitalboard().getHospitals().add(new Hospital("Hospital Linksboven", new Location(0, 100, "LB")));
		dispatchCenter.getGarage().getHospitalboard().getHospitals().add(new Hospital("Hospital Linksonder", new Location(0, 0, "LO")));
		dispatchCenter.getGarage().getHospitalboard().getHospitals().add(new Hospital("Hospital rechtsboven", new Location(100, 100, "RB")));
		dispatchCenter.getGarage().getHospitalboard().getHospitals().add(new Hospital("Hospital rechtsonder", new Location(100, 0, "RO")));
	}	*/


}
